package com.aspire.boc.mms.wmf.cam;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.aspire.cam.rpc.client.XmlRpcClientProxy;


@SuppressWarnings("serial")
public class FinePermission implements Serializable {

	private Log logger = LogFactory.getLog(FinePermission.class);

	private boolean isSystemAdmin = false;

	private List<Integer> projectPermissionList = new ArrayList<Integer>();

	private List<Integer> productAdminPermissionList = new ArrayList<Integer>();

	private List<Integer> productEditPermissionList = new ArrayList<Integer>();

	private List<Integer> productSendPermissionList = new ArrayList<Integer>();

	private List<Integer> productAffirmPermissionList = new ArrayList<Integer>();

	private List<Integer> productReadPermissionList = new ArrayList<Integer>();



	/**
	 * 构造函数
	 * 
	 * @param userid
	 *            String 用户id
	 * @param system_def
	 *            String 系统定义
	 * @param url
	 *            String 权限系统地址
	 */
	@SuppressWarnings("unchecked")
	public FinePermission(String userid, String system_def, String url) {
		logger.info("[userid:" + userid + "][system_def:" + system_def
				+ "][url:" + url + "]");
		long time = System.currentTimeMillis();
		try {
			logger.info("[" + userid + "][getPermList start]");
			XmlRpcClientProxy client = new XmlRpcClientProxy(url);
			// 取得用户粗粒度权限
			Vector<Object> params = new Vector<Object>();
			params.add(system_def);
			params.add(userid);
			Vector<Map> permList = (Vector) client.execute(
					"UserPermService.getPermList", params);
			logger.info("[" + userid + "][getPermList end][cost:"
					+ (System.currentTimeMillis() - time) + "]");
			for (Map map : permList) {
				String res_string = (String) map.get("res_string");
				// 判断是否为系统管理员
				if ("SystemAdmin".equals(res_string))
					isSystemAdmin = true;
			
			}
			// 非系统管理员取得细粒度权限
			if (!isSystemAdmin) {
				// 获得项目细粒度权限
				params = new Vector<Object>();
				params.add(system_def);
				params.add("ProjectAdmin");
				params.add(userid);
				logger.info("[" + userid + "][getProjectAdmin start]");
				time = System.currentTimeMillis();
				Vector<Map> projectPermList = (Vector) client.execute(
						"FineGrainedResService.getPermList", params);
				logger.info("[" + userid + "][getProjectAdmin end][cost:"
						+ (System.currentTimeMillis() - time) + "]");
				for (Map map : projectPermList) {
					String operation = (String) map.get("operation");
					int projectID = Integer.parseInt((String) map
							.get("data_id"));
					// 获得项目管理员权限列表
					if ("admin".equals(operation)) {
						if (!projectPermissionList.contains(projectID))
							projectPermissionList.add(projectID);
					}
					
				}
				// logger.debug("projectPermList--" + projectPermList);
				logger.info("[" + userid + "][projectPermissionList:"
						+ projectPermissionList + "]");
				

				// 获得产品细粒度权限列表
				params = new Vector<Object>();
				params.add(system_def);
				params.add("ProductAdmin");
				params.add(userid);
				logger.info("[" + userid + "][getProductAdmin start]");
				time = System.currentTimeMillis();
				Vector<Map> productPermList = (Vector) client.execute(
						"FineGrainedResService.getPermList", params);
				logger.info("[" + userid + "][getProductAdmin end][cost:"
						+ (System.currentTimeMillis() - time) + "]");
				for (Map map : productPermList) {
					String operation = (String) map.get("operation");
					int productID = Integer.parseInt((String) map
							.get("data_id"));
					// 获得产品管理员权限列表
					if ("admin".equals(operation)) {
						if (!productAdminPermissionList.contains(productID))
							productAdminPermissionList.add(productID);
					}
					// 获得产品编辑权限列表
					else if ("edit".equals(operation)) {
						if (!productEditPermissionList.contains(productID))
							productEditPermissionList.add(productID);
					}
					// 获得产品发送权限列表
					else if ("send".equals(operation)) {
						if (!productSendPermissionList.contains(productID))
							productSendPermissionList.add(productID);
					}
					// 获得产品审核权限列表
					else if ("affirm".equals(operation)) {
						if (!productAffirmPermissionList.contains(productID))
							productAffirmPermissionList.add(productID);
					}
					// 获得产品只读权限列表
					else if ("read".equals(operation)) {
						if (!productReadPermissionList.contains(productID))
							productReadPermissionList.add(productID);
					}
				
				}

				// logger.debug("productPermList--" + productPermList);
				logger.info("[" + userid + "][productAdminPermissionList:"
						+ productAdminPermissionList + "]");
				logger.info("[" + userid + "][productEditPermissionList:"
						+ productEditPermissionList + "]");
				logger.info("[" + userid + "][productSendPermissionList:"
						+ productSendPermissionList + "]");
				logger.info("[" + userid + "][productAffirmPermissionList:"
						+ productAffirmPermissionList + "]");
				logger.info("[" + userid + "][productReadPermissionList:"
						+ productReadPermissionList + "]");
				
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}


	/**
	 * 获得用户拥有产品管理权限的产品id列表
	 * 
	 * @return List
	 */
	public List<Integer> getProductAdminPermissionList() {
		return productAdminPermissionList;
	}

	/**
	 * 获得用户拥有产品审批权限的产品id列表
	 * 
	 * @return List
	 */
	public List<Integer> getProductAffirmPermissionList() {
		return productAffirmPermissionList;
	}

	/**
	 * 获得用户拥有产品编辑权限的产品id列表
	 * 
	 * @return List
	 */
	public List<Integer> getProductEditPermissionList() {
		return productEditPermissionList;
	}

	/**
	 * 获得用户拥有产品权限的产品id列表
	 * 
	 * @return List
	 */
	// public List<Integer> getProductPermissionList() {
	// return productPermissionList;
	// }
	/**
	 * 获得用户拥有产品发送权限的产品id列表
	 * 
	 * @return List
	 */
	public List<Integer> getProductSendPermissionList() {
		return productSendPermissionList;
	}

	/**
	 * 获得用户拥有产品只读权限的产品id列表
	 * 
	 * @return List
	 */
	public List<Integer> getProductReadPermissionList() {
		return productReadPermissionList;
	}


	/**
	 * 获得用户拥有项目权限的项目id列表
	 * 
	 * @return List
	 */
	public List<Integer> getProjectPermissionList() {
		return projectPermissionList;
	}

	/**
	 * 判断用户是否为管理员
	 * 
	 * @return boolean
	 */
	public boolean isSystemAdmin() {
		return isSystemAdmin;
	}

	/**
	 * 判断用户是否为项目管理员
	 * 如果projectID=0，则判断用户是否有项目管理员的权限；如果projectID不为0，则判断用户是否为某一个项目的项目管理员
	 * 
	 * @param projectID
	 *            int
	 * @return boolean
	 */
	public boolean isProjectAdmin(int projectID) {
		if (projectID != 0) {
			if (projectPermissionList.contains(projectID))
				return true;
			else
				return false;
		} else {
			if (projectPermissionList != null
					&& projectPermissionList.size() > 0)
				return true;
			else
				return false;
		}
	}

	
	/**
	 * 判断用户是否为产品管理员
	 * 如果productID=0，则判断用户是否有产品管理员的权限；如果productID不为0，则判断用户是否为某一个产品的产品管理员
	 * 
	 * @param productID
	 *            int 产品id
	 * @return boolean
	 */
	public boolean isProductAdmin(int productID) {
		if (productID != 0) {
			if (productAdminPermissionList.contains(productID))
				return true;
			else
				return false;
		} else {
			if (productAdminPermissionList != null
					&& productAdminPermissionList.size() > 0)
				return true;
			else
				return false;

		}
	}

	
}
